<!-- /* ############ BEGIN SELECT GAME ############## */ -->

<?php
require_once  __DIR__ . '/includes/authenticate.php';

if (isset($_POST['season'])){ 

include_once './db_bb_connect.php';

$season = $_POST['season'];

?>


<Script>
  function setSeason(){
  document.getElementById("selected-season").innerHTML = "Season <?= $_POST['season']; ?> is selected";
}
  setSeason();

</Script>

  <div class="container-fluid">
  <div class="row">
  <div class="col-lg-3 col-md-3"></div>
  <div class="col-lg-6 col-md-6">
  

  <form method="post" action="" > 

 <select name="selectedgame" class="form-control" >
         <option id="selected-game" value="" disabled="disabled" selected="selected">Select a game</option>

<?php 
$sql = "SELECT gamenumber, date, opponent, location
FROM games
WHERE season = '$season'
ORDER BY date DESC";

$result=$db->query($sql);

while($row=$result->fetch_assoc()){ 
    $gamenumber = $row['gamenumber'];
    $date = $row['date'];
    $opponent = $row['opponent'];
    $location = $row['location'];

  echo "<option value='$gamenumber'>Game $gamenumber $date ($opponent $location)  </option>";
}

?>
  </select>
  <input type="hidden" name="season" value="<?= $_POST['season']; ?>"/>
</form>
<br>


  </div>
  <div class="col-lg-3 col-md-3"></div>
  </div>
  </div>
  


<?php } ?>

<!-- /* ############ END SELECT GAME ############## */ -->


<!-- /* ############ BEGIN ADD GAME ############## */ -->
<?php 

if (isset($_POST['addgamenumber']) || isset($_POST['addgameopp']) ) {
  // SQL QUERY TO ADD GAME TO DB HERE
  header( 'Location: ./');
  exit;
}
?>
<!-- /* ############ END ADD GAME ############## */ -->


  </div>

  <div id="addstatshandler2content"> </div>
  <div class="col-md-1 col-sm-3 col-xs-2"></div>

  </div>  
  </div>

  <!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 

  <script type="text/javascript">
  $("form").on("submit", function(e) {
    postData = $( this ).serialize();
      e.preventDefault();
      callAddStatsPage2(postData);
});


$( "select" ).on( "change", function(e) {
  postData = $( this.form ).serialize();
  callAddStatsPage2(postData);
  e.preventDefault();
});

function callAddStatsPage2(e, postData){
  $.ajax({
  type: "POST",
  url: "./addstats_bytotals_selectplayer.php",
  data: this.postData,
  success: function(data) {
  $("#addstatshandler2content").html(data);
  }
  }); 

  e.preventDefault();

};


</script>